<?php

/**
 * Miffos framework
 * ----------------------------------------------
 *
 * Copyright (c) 2008, 2009 Martin Sadový (http://sodae.cz)
 *
 * This source file is subject to the "Miffos license" that is bundled
 * with this package in the file license.txt.
 *
 * @copyright  Copyright (c) 2008, 2009 Martin Sadový
 * @license    license.txt
 * @link       http://miffos.sodae.cz
 * @package    miffos
 * @version    $Id$
 */

require_once dirname(__FILE__).'/Rule.php';
require_once dirname(__FILE__).'/Controls/Text.php';
require_once dirname(__FILE__).'/Controls/Button.php';
require_once dirname(__FILE__).'/Controls/File.php';
require_once dirname(__FILE__).'/Controls/Checkbox.php';
require_once dirname(__FILE__).'/Controls/Select.php';
require_once dirname(__FILE__).'/Controls/Radio.php';

  abstract class FormControl{
  
    protected $element;
    
    protected $label;

    protected $name;

    private $value;

    private $rule;

    private $error;

    public function __construct($form, $name, $label)
    {
      $this->element = Html::el($this->tag);
      $this->label   = Html::el($this->label,$label);
      $this->rule    = new FormRule($form);
      $this->name    = $name;
      $this->element->name =  $form->getName().'['.$name.']';
      $this->error =  Html::el('div');
    }



    /**
     * Set Label
     * @param string or instance Html $label
     */
    public function setLabel($label)
    {
      $this->label->setText($label);
    }



    /**
     * Return label
     * @return string or instance Html
     */
    public function getLabel()
    {
      return isset($this->label)?$this->label:null;
    }



    /**
     * return html instance for edit html element in element
     * @return object
     */
    public function getPrototype()
    {
      return $this->element;
    }



    /**
     * Return value after submit
     * @return string or null
     */
    public function getValue()
    {
        return $this->value;
    }



    /**
     * Set value
     * @param string or null
     */
    public function setValue($value)
    {
        $this->value = $value;
    }



    /**
     * Get name
     */
    public function getName()
    {
        return $this->name;
    }



    /**
     * Add Rule for Element
     * @param $
     */
    public function addRule($rule, $massage, $data=null)
    {
        $this->rule->addRule($this, $rule, $data, $massage);
    }


    
    /**
     * Return is valid
     * @return bool
     */
    public function isValid()
    {
        return $this->rule->validation();
    }



    /**
     * Get error html instance
     * @return bool
     */
    public function getError()
    {
        return $this->error;
    }



    /**
     * Get errors from Rule
     * @return bool
     */
    public function getRuleErrors()
    {
        return $this->rule->getError();
    }



    /**
     * Render
     * @return string
     */
    public function render()
    {
        return $this->element->render();
    }



    public function __toString()
    {
        return $this->getValue();
    }
  }

  class FormFreeSpace extends FormControl{
      
  }
?>